package org.xyl.game.controller;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xyl.game.dto.InquireRoomRet;
import org.xyl.game.service.RoomService;
import org.xyl.game.tool.GsonUtil;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
public class RoomController {

    @Autowired
    private RoomService roomService;


    @PostMapping("/createroom")
    @ResponseBody
    public String createRoom(String username) {
        Map<String, String> ansMap = new HashMap<>();
        String room = roomService.createRoom(username);
        ansMap.put("roomid", room);
        return GsonUtil.getGsonBuilder().create().toJson(ansMap);
    }

    @PostMapping("/joinroom")
    @ResponseBody
    public String joinRoom(String roomid, String username, String team) {
        Map<String, String> ansMap = new HashMap<>();
        boolean b = roomService.joinRoom(Integer.parseInt(roomid), username, team);
        if (!b) {
            ansMap.put("state", "fail");
            return GsonUtil.getGsonBuilder().create().toJson(ansMap);
        }
        ansMap.put("state", "success");
        return GsonUtil.getGsonBuilder().create().toJson(ansMap);
    }

    @PostMapping("/inquire")
    @ResponseBody
    public String inquireRoom(String roomid) {
        InquireRoomRet inquireRoomRet = roomService.inquireRoom(Integer.parseInt(roomid));
        return GsonUtil.getGsonBuilder().create().toJson(inquireRoomRet);
    }
}
